﻿using System;
using SCG = System.Collections.Generic;

using Nemerle.Assertions;
using Nemerle.Utility;

namespace Sample.Utils {

    public module MathUtils {

        public IsPowerOf2(val : long) : bool {
            unchecked {
                (val & -val) - val == 0 && val != 0
            }
        }

        public RoundToPowerOf2(val : long) : long {
            unchecked {
                mutable x = val;
                x -= 1;
                x |= x >> 1;
                x |= x >> 2;
                x |= x >> 4;
                x |= x >> 8;
                x |= x >> 16;
                x |= x >> 32;
                x |= x >> 64;
                x |= x >> 128;
                x + 1
            }
        }

    }

}